﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Refix.Core.Repository
{
    [Serializable]
    public class Reference
    {
        [XmlIgnore]
        public string ResolvedFullAssemblyName
        {
            get
            {
                var builder = new StringBuilder();
                builder.Append(AssemblyName);
                AddPartIfNotNullOrEmpty(builder, ResolvedVersion, "Version");
                AddPartIfNotNullOrEmpty(builder, Culture, "Culture");
                AddPartIfNotNullOrEmpty(builder, PublicKeyToken, "PublicKeyToken");
                AddPartIfNotNullOrEmpty(builder, ProcessorArchitecture, "processorArchitecture");
                return builder.ToString();
            }
        }

        [XmlIgnore]
        public string FullAssemblyName
        {
            get
            {
                var builder = new StringBuilder();
                builder.Append(AssemblyName);
                AddPartIfNotNullOrEmpty(builder, SpecifiedVersion, "Version");
                AddPartIfNotNullOrEmpty(builder, Culture, "Culture");
                AddPartIfNotNullOrEmpty(builder, PublicKeyToken, "PublicKeyToken");
                AddPartIfNotNullOrEmpty(builder, ProcessorArchitecture, "processorArchitecture");
                return builder.ToString();
            }
            set
            {
                string[] parts = value.Split(',');
                AssemblyName = parts[0];
                for (int i = 1; i < parts.Length; i++)
                {
                    string thisPart = parts[i].Trim();
                    if (!thisPart.Contains("="))
                    {
                        continue;
                    }
                    string[] partParts = thisPart.Split('=');
                    string partValue = partParts[1].Trim();
                    switch (partParts[0].Trim().ToUpperInvariant())
                    {
                        case "VERSION":
                            SpecifiedVersion = partValue;
                            break;
                        case "CULTURE":
                            Culture = partValue;
                            break;
                        case "PUBLICKEYTOKEN":
                            PublicKeyToken = partValue;
                            break;
                        case "PROCESSORARCHITECTURE":
                            ProcessorArchitecture = partValue;
                            break;
                    }
                }
            }
        }

        public string AssemblyName { get; set; }
        public string SpecifiedVersion { get; set; }
        public string ResolvedVersion { get; set; }
        public string Culture { get; set; }
        public string PublicKeyToken { get; set; }
        public string ProcessorArchitecture { get; set; }
        public bool SpecificVersion { get; set; }
        public string HintPath { get; set; }

        [XmlIgnore]
        public XElement Node { get; set; }

        static private void AddPartIfNotNullOrEmpty(StringBuilder builder, string value, string label)
        {
            if (!string.IsNullOrEmpty(value))
            {
                builder.AppendFormat(", {1}={0}", value, label);
            }
        }
    }
}